#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_set>
using namespace std;


struct ListNode{
    int val;
    ListNode* next;
    ListNode(int x): val(x), next(nullptr){}
};

class Solution{
    public:

    ListNode* ans1(ListNode* head, int n){
        ListNode* temp = head;
        ListNode* newhead = new ListNode(-1);
        newhead->next = head;
        int len = 0;
        while(temp){
            temp = temp->next;
            len++;
        }
        int index = len - n;
        int i = 0;
        temp = newhead;
        while(temp){
            if(i == index){
                temp->next = temp->next->next;
                temp = temp->next;
            }else{
                temp = temp->next;
            }
            i++;
        }
        return newhead->next;
    }

};

void test1(){
    vector<int> num = {1,2,3,4,5};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int n = 2;

    Solution s;
    ListNode* ret = s.ans1(head, n);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test2(){
    vector<int> num = {1};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int n = 1;

    Solution s;
    ListNode* ret = s.ans1(head, n);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}

void test3(){
    vector<int> num = {1, 2};
    ListNode* head = nullptr;
    ListNode* tail = nullptr;
    for(int i = 0; i != num.size(); i++){
        if(!head){
            head = tail = new ListNode(num[i]);
        }else{
            tail->next = new ListNode(num[i]);
            tail = tail->next;
        }
    }
    int n = 1;

    Solution s;
    ListNode* ret = s.ans1(head, n);
    while(ret){
        cout << ret->val << " " << ends;
        ret = ret->next;
    }
    cout << endl;
}


int main(void){
    test1();
    test2();
    test3();

    return 0;
}